package engine.newUi
{
	import com.greensock.TweenLite;
	import com.greensock.TweenMax;
	
	import engine.data.BindManage;
	import engine.data.ModelLocator;
	import engine.data.ResPath;
	import engine.tip.TipsMainLogic;
	
	import flash.events.MouseEvent;
	import flash.text.engine.RenderingMode;
	import flash.utils.setTimeout;
	
	import newx.com.ResManager;
	import newx.com.config.StaticIni;
	import newx.com.control.NxClip;

	/**
	 * 预告悬浮引导  
	 * @author wanghb
	 * 
	 */
	public class NextActivityGuideLogic
	{
		private static var _self:NextActivityGuideLogic
		
		private var _myDis				:NextActivityGuideUI;
		private var _bindManage		:BindManage;
		private var _model				:ModelLocator;
		private var _level				:int
		private var _nextActivityObj	:Object;
		private var _nextActivityTips	:Object;
		private var _checkInfoArray	:Array;
		private var _checkItemPic		:NxClip;
		private var _checkOldPic		:NxClip;
		private var _resfhArray		:Array
		private var _oldLevel			:int	= 0;
		public function NextActivityGuideLogic()
		{
			init();
		}
		public static function getInstance():NextActivityGuideLogic
		{
			if(_self == null)
			{
				_self = new NextActivityGuideLogic();
			}
			return _self;
		}
		/**
		 * 设定等级 
		 * @param value
		 * 
		 */		
		public function set level(value:int):void
		{
			_level = checkLevel(value);
			if(_level == -1)
			{
				_myDis.imageSpace.visible	=	false;
			}
			else
			{
				if(_oldLevel == 0)
				{
					_oldLevel	=	_level	
					_myDis.imageSpace.visible	=	true;
					setLevelNum(String(_level));
					setLevelInfo(_nextActivityObj[String(_level)]);
				}
				else
				{
					if(_level != _oldLevel)
					{
						_oldLevel	=	_level
						TweenLite.to(_myDis.imageSpace,.5,{x:210,y:0,onComplete:showInfo});
					}
				}

			}
		}
		/**
		 * 显示信息 
		 */		
		private function showInfo():void
		{
			setLevelNum(String(_level));
			setLevelInfo(_nextActivityObj[String(_level)]);
			TweenLite.to(_myDis.imageSpace,1,{ x:0,y:0,onComplete:function():void
			{
				_myDis.back.play();
				setTimeout(function():void
				{
					_myDis.back.gotoAndStop(_myDis.back.totalFrames);
				},8000)
			}
			});
			
		}
		private function init():void
		{
			_nextActivityObj	=	StaticIni.getIniObj("NextActivityGuide") as Object;
			_nextActivityTips	=	StaticIni.getIniObj("NextActivityGuideTips") as Object;
			_model				=	ModelLocator.getInstance();
			_bindManage			=	BindManage.getInstance();
			_myDis				=	NextActivityGuideUI.getInstance();	
			_myDis.addChild(_myDis.imageSpace);
			_myDis.imageSpace.addChild(_myDis.back);
			_myDis.imageSpace.addChild(_myDis.lv_num_0);
			_myDis.imageSpace.addChild(_myDis.lv_num_1);
			_myDis.imageSpace.addChild(_myDis.title_0);
			_myDis.imageSpace.addChild(_myDis.info_0);
			_myDis.imageSpace.addChild(_myDis.info_1);
			_myDis.imageSpace.addChild(_myDis.info_2);
			_myDis.imageSpace.addChild(_myDis.info_3);
			_myDis.imageSpace.addChild(_myDis.info_4);
			_bindManage.BindProp(_model.scene.Role,"Level",this,"level");
			_myDis.addEventListener(MouseEvent.MOUSE_OVER,onOver);
			_myDis.addEventListener(MouseEvent.MOUSE_OUT,onOut);
		}
		
		private function onOver(e:MouseEvent):void
		{
			if(_nextActivityTips[_level]) TipsMainLogic.instance.showString(_nextActivityTips[_level]);
		}
		
		private function onOut(e:MouseEvent):void
		{
			TipsMainLogic.instance.hide();
		}
		
		/**
		 * 设定等级显示所对应的图片
		 */		
		private function setLevelNum(str:String):void
		{
			_myDis.lv_num_0.visible	=	false;
			_myDis.lv_num_1.visible	=	false;
			for(var i:int = 0; i< str.length; i++)
			{
				_checkItemPic			=	_myDis["lv_num_" + i] as NxClip
				_checkItemPic.source	=	ResPath.PNG + str.substr(i,1) + ".png"
				_checkItemPic.visible	=	true;
			}
		}
		/**
		 * 显示对应的活动信息 
		 * @param str
		 * 
		 */		
		private function setLevelInfo(str:String):void
		{
			_checkInfoArray	=	str.split(",");
			_myDis.title_0.source	=	ResPath.PNG + _checkInfoArray.shift() + ".png";
			//还原
			for(var i:int = 0; i< 5; i++)
			{
				_checkItemPic	=	_myDis["info_" + i] as NxClip;
				_checkItemPic.source	=	null;
				_checkItemPic.visible	=	false;
			}
			//显示
			for(var j:int = 0; j< _checkInfoArray.length; j++)
			{
				_checkItemPic	=	_myDis["info_" + j] as NxClip;
				_checkItemPic.source	=	ResPath.PNG + _checkInfoArray[j] + ".png";
				_checkItemPic.visible	=	true;
			}
//			if(_myDis.lv_num_1.visible)
//			{
//				_myDis.title_0.x	=	830;
//			}
//			else
//			{
//				_myDis.title_0.x	=	815;
//			}
		}
		/**
		 * 需要显示的等级信息 
		 * @return 
		 * 
		 */		
		private function checkLevel(val:int):int
		{
			for(var i:int = val+1; i<=90; i++)
			{
				if(_nextActivityObj[i] != null)
				{
					return i;
				}
			}
			return -1
		}
	}
}